package com.isoft.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.isoft.mapper.LifecycleRecordMapper;
import com.isoft.pojo.dto.LifecycleRecordQueryDTO;
import com.isoft.pojo.vo.LifecycleRecordVO;
import com.isoft.result.CycPageResult;
import com.isoft.service.LifecycleRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class LifecycleRecordServiceImpl implements LifecycleRecordService {
    @Autowired
    private LifecycleRecordMapper lifecycleRecordMapper;
    /**
     * 分页查询生命周期记录
     */
    public CycPageResult pageLifecycleRecord(LifecycleRecordQueryDTO dto) {
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;

        // 修复点：添加空值检查
        if (dto.getStartDate() != null) {
            startTime = dto.getStartDate().atTime(0, 0, 0);
        }
        if (dto.getEndDate() != null) {
            endTime = dto.getEndDate().atTime(23, 59, 59);
        }

        PageHelper.startPage(dto.getPage(), dto.getSize());
        Page<LifecycleRecordVO> page = lifecycleRecordMapper
                  .pageQuery(
                        dto.getKeyword(),
                        dto.getSystemId(),
                        dto.getOperationType(),
                        startTime, endTime);
        return new CycPageResult(page.getTotal(), page.getResult());
    }
}
